package com.twu.biblioteca.util;

import java.util.HashMap;
import java.util.Iterator;

public class ConsoleInterface implements IConsoleInterface{

    public HashMap<String,IMenuEntry> menuEntryHashMap;

    public ConsoleInterface() {
        menuEntryHashMap = new HashMap<String, IMenuEntry>();
        menuEntryHashMap.put("list",new ListMenu());
        menuEntryHashMap.put("checkout", new CheckOutMenu());
        menuEntryHashMap.put("return",new ReturnMenu());
        menuEntryHashMap.put("login",new LoginMenu());
    }


    public void displayMenu() {
        if(UserAuthenticator.getActiveUser() == null) {
            System.out.println("Use Login Username Password");
            return;
        }

        System.out.println("Use   [ command      bookName/bookCode]");
        Iterator<String> iterator = menuEntryHashMap.keySet().iterator();
        System.out.println("Choose One of the commands : ");
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    @Override
    public void run(String args) {
        String[] strArray = args.split(" ",2);
        String command = strArray[0].toLowerCase();
        String arguement = (strArray.length == 1)? "":strArray[1].toLowerCase();
        IMenuEntry menuEntry = menuEntryHashMap.get(command);
        try {
        System.out.println(menuEntry.handle(arguement));
        } catch( NullPointerException e) {
            System.out.println("Enter a valid option");
        }
    }
}
